//
//  XDBankCordChangeController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/21.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDBankCordChangeController.h"
#import "UIImageView+WebCache.h"
#import "XDBankCordChangeCell.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDBankCardChangeLostView.h"
#import "XDAlertView.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
@interface XDBankCordChangeController ()<UITableViewDelegate, UITableViewDataSource,XDBankCordChangeDelegate,XDBankCardChangeLostViewDelegate>

@property (nonatomic, weak) UITableView *tableView;


@property (nonatomic, weak) UIView *accessoryView;

@property (nonatomic, assign) BOOL isVisible;

@property (nonatomic, assign) BOOL isRotation;

@end

@implementation XDBankCordChangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
    
}



- (void)setupUI {
    
    self.title = @"更换银行卡";
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
    tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.tableView = tableView;
    self.tableView.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"XDUserCenterCell";
    UITableViewCell *cell;
//    = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:myID];
    }
    cell.textLabel.textColor = kColorRGB(102,102,102);
    cell.textLabel.font = kBaseFont_(14);
    if (indexPath.section == 0) {
        cell.textLabel.text = @"当前银行卡为:";
        cell.detailTextLabel.textColor = kColorRGB(51, 51, 51);
        cell.detailTextLabel.font =kBaseFont_(16);
//        bankInfo =         {
//            bankCode = ccb;
//            bankName = "\U5efa\U8bbe\U94f6\U884c";
//            bankNumber = 6509;
//        };
        NSString *bankNumber = [self.bankInfo[@"bankNumber"] substringWithRange:NSMakeRange([self.bankInfo[@"bankNumber"] length] - 4, 4)];
             NSString *bank = [NSString stringWithFormat:@"%@(%@)", self.bankInfo[@"bankName"],bankNumber];
//        {
//            accountCity = "<null>";
//            accountProvince = "<null>";
//            bankCardType = "\U501f\U8bb0\U5361";
//            bankCode = abc;
//            bankIconUrl = "http://172.16.20.248:6060/zhrtChinaWeb/biz-assets/bankIcon/small/abc.png";
//            bankId = 53;
//            bankLimit = "\U6bcf\U65e53w,\U6bcf\U6b215K";
//            bankName = "\U519c\U4e1a\U94f6\U884c";
//            bankNumber = "6228 **** **** 4575";
//            bankPhone = 15244608254;
//            bankType = 1;
//            brachName = "<null>";
//        }

        cell.detailTextLabel.text = bank;
        CGRect rect = [bank boundingRectWithSize:CGSizeMake(MAXFLOAT, 50) options:0 attributes:@{NSFontAttributeName: kBaseFont_(16)} context:nil];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenW - rect.size.width - 15 - 18 - 8, 16, 18, 18)];
        [img sd_setImageWithURL:[NSURL URLWithString:self.bankInfo[@"bankIconUrl"]]];
        [cell.contentView addSubview:img];
        
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nextInd"]];
        imgView.frame = CGRectMake(kScreenW - 15 - 6, (50 - 11) * 0.5, 6, 11);
        [cell.contentView addSubview:imgView];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 50)];
        lb.text = @"银行卡可用,想换张新卡";
        lb.textColor = kColorRGB(102,102,102);
        lb.font = kBaseFont_(14);
        [cell.contentView addSubview:lb];
        if (self.isVisible) {
            imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
            imgView.contentMode = UIViewContentModeCenter;
            imgView.clipsToBounds = NO;
            
            XDBankCordChangeCell *change = [[NSBundle mainBundle] loadNibNamed:@"XDBankCordChangeCell" owner:self options:nil].firstObject;

            change.frame = CGRectMake(0, 50, kScreenW, 310);
            
            change.delegate = self;
            
            [cell.contentView addSubview:change];
 
        } else {
            
            imgView.transform  = CGAffineTransformMakeRotation(0);
            
        }
    }else if (indexPath.section == 1 && indexPath.row == 1) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nextInd"]];
        imgView.frame = CGRectMake(kScreenW - 15 - 6, (50 - 11) * 0.5, 6, 11);
        [cell.contentView addSubview:imgView];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 50)];
        lb.text = @"银行卡丢失或已注销";
        lb.textColor = kColorRGB(102,102,102);
        lb.font = kBaseFont_(14);
        [cell.contentView addSubview:lb];
        if (self.isRotation) {
            imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
            imgView.contentMode = UIViewContentModeCenter;
            imgView.clipsToBounds = NO;
            XDBankCardChangeLostView *lostView = [[NSBundle mainBundle] loadNibNamed:@"XDBankCardChangeLostView" owner:self options:nil].firstObject;
            lostView.frame = CGRectMake(0, 50, kScreenW, 310);
            
            lostView.delegate = self;
            [cell.contentView addSubview:lostView];
            
        } else {
            
            imgView.transform  = CGAffineTransformMakeRotation(0);

        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        self.isVisible = !self.isVisible;
     
       [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        self.isRotation = !self.isRotation;
        
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isVisible && indexPath.row == 0 && indexPath.section == 1) {
        
        return 360;
    }else if (self.isRotation && indexPath.row == 1 && indexPath.section == 1) {
        
        return 390;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 38;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 38)];
        view.backgroundColor = kColorRGB(234, 234, 234);
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 38)];
        lb.text = @"请选择更换银行卡的原因：";
        lb.textColor = kColorRGB(102,102,102);
        lb.font = kBaseFont_(13);
        [view addSubview:lb];
        return view;
    }
    
    return [UIView new];
}



- (void)deleteBankCardWithBankCordChange:(XDBankCordChangeCell *)change {
    
    [self deleteBankCard];
    NSLog(@"删除银行卡");
}



- (void)deleteBankCard {
    
    NSDictionary * param = @{@"method":@"undoBindBankCard",@"toDelBindBankCard":self.bankInfo[@"bankId"]};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
      
//            @"无可用余额,确认删除?"
//            @"账户有余额,提现后可删除" 
   
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            if ([responseObject[@"result"][@"state"] isEqualToString:@"yes"]) {
                
                XDAlertView *alert = [[XDAlertView alloc] initWithTitle:nil message:@"无可用余额,确认删除?" sureBtn:@"确定" cancleBtn:@"取消"];
                alert.resultIndex = ^(NSInteger index){
                    if (index == 1) {
                        
                        
                    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
                        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
                        
                    [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                [self.navigationController popViewControllerAnimated:YES];
                            });
                        
                        
                        } else {
                            
                    [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
                        }
                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                        
                        
                        
                    }];
                    
                    
                    }
                };
                [alert showXLAlertView];
                
            } else {
                
                XDAlertView *alert = [[XDAlertView alloc] initWithTitle:nil message:@"账户有余额,提现后可删除" sureBtn:@"去提现" cancleBtn:@"取消"];
                alert.resultIndex = ^(NSInteger index){
                    //回调---处理一系列动作
                    if (index == 1) {
                      NSLog(@"去提现");  
                    }
                    
                };
                [alert showXLAlertView];
                
                
            }
            
            
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)telWithBankCardChangeLostView:(XDBankCardChangeLostView *)lostView {
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"400-805-9696"];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
 
}


@end
